/*
* $Id: EvacuationTable.cpp 2 2009-12-23 13:58:02Z volok-aleksej@yandex.ru $
*
* Copyright 2009 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "EvacuationTable.h"
#include "TableNames.h"
#include "../SqlBinder.h"

CEvacuationTable::CEvacuationTable()
: CTable(EVACUATION_TABLE_NAME){}
CEvacuationTable::~CEvacuationTable(){}

void CEvacuationTable::CreateTable(IDatabase* pDatabase){
	std::tstring strRequest(_T("CREATE TABLE IF NOT EXISTS "));
	pDatabase->ExecRequest(strRequest + GetNameTable() + 
						_T("(date DATE NOT NULL,")\
						_T("id_family INTEGER NOT NULL,")\
						_T("honey INTEGER NOT NULL,")\
						_T("plant TEXT NULL,")\
						_T("CONSTRAINT ftEvacuation")\
						_T("	FOREIGN KEY(id_family)")\
						_T("	REFERENCES families(id_family));"));
}
void CEvacuationTable::UpdateValue(IDatabase* pDatabase){
	std::tstring strRequest(_T("UPDATE "));
	CSqlBinder binder(strRequest + GetNameTable() + 
					_T(" SET honey=?, plant=? WHERE id_family=? AND date=?;"));
	binder.SetValue(1, m_content.s_strPlant);
	binder.SetValue(3, m_content.s_date, true);
	binder.SetValue(0, m_content.s_nHoney);
	binder.SetValue(2, m_content.s_nIdFamily);
	pDatabase->ExecRequest(binder.GetRequest());
	m_content.Clear();
}
IResult* CEvacuationTable::GetValue(IDatabase* pDatabase){
	CSqlBinder binder;
	std::tstring strRequest(_T("SELECT * FROM "));
	if(m_content.s_date){
		binder.SetRequest(strRequest + GetNameTable() + _T(" WHERE date=?"));
		binder.SetValue(0, m_content.s_date, true);
	}else if(m_content.s_nIdFamily){
		binder.SetRequest(strRequest + GetNameTable() + _T(" WHERE id_family=?"));
		binder.SetValue(0, m_content.s_nIdFamily);
	}else
		binder.SetRequest(strRequest + GetNameTable());
	m_content.Clear();
	return FillContent(pDatabase->ExecRequest(binder.GetRequest()));
}
void CEvacuationTable::SetValue(IDatabase* pDatabase){
	CSqlBinder binder;
	std::tstring strRequest(_T("INSERT INTO "));
	binder.SetRequest(strRequest + GetNameTable() + _T(" VALUES(?, ?, ?, ?)"));
	binder.SetValue(0, m_content.s_date, true);
	binder.SetValue(1, m_content.s_nIdFamily);
	binder.SetValue(2, m_content.s_nHoney);
	if(m_content.s_strPlant.empty())
		binder.SetValue(3);
	else
		binder.SetValue(3, m_content.s_strPlant);
	m_content.Clear();
	pDatabase->ExecRequest(binder.GetRequest());
}
void CEvacuationTable::DeleteValue(IDatabase* pDatabase){
	CSqlBinder binder;
	std::tstring strRequest(_T("DELETE FROM "));
	if(m_content.s_date){
		strRequest = strRequest + GetNameTable() + _T(" WHERE date=?");
		binder.SetValue(0, m_content.s_date, true);
		if(m_content.s_nIdFamily){
			strRequest = strRequest + _T(" AND id_family=?");
			binder.SetValue(1, m_content.s_nIdFamily);
		}
		binder.SetRequest(strRequest);
	}else if(m_content.s_nIdFamily){
		strRequest = strRequest + GetNameTable() + _T(" WHERE id_family=?");
		binder.SetValue(0, m_content.s_nIdFamily);
		binder.SetRequest(strRequest);
	}else{
		binder.SetRequest(strRequest + GetNameTable());
	}
	m_content.Clear();
	pDatabase->ExecRequest(binder.GetRequest());
}
IOperation::i_content* CEvacuationTable::CreateContent(){
	return new content;
}

CEvacuationTable::content::content(){
	Clear();
}
void CEvacuationTable::content::Clear(){
	s_strPlant.clear();
	s_date = 0;
	s_nHoney = 0;
	s_nIdFamily = 0;
}
void CEvacuationTable::content::Set(const std::vector<CTableItem>& aContent){
	for(std::vector<CTableItem>::const_iterator it = aContent.begin(); it != aContent.end(); it++){
		if(it->m_strNameColumn == _T("date"))
			s_date = CConvertFunction::ConvertFromDate(it->m_strValueColumn);
		else if(it->m_strNameColumn == _T("id_family"))
			s_nIdFamily = _ttoi(it->m_strValueColumn.c_str());
		else if(it->m_strNameColumn == _T("honey"))
			s_nHoney = _ttoi(it->m_strValueColumn.c_str());
		else if(it->m_strNameColumn == _T("plant"))
			s_strPlant = it->m_strValueColumn;
	}
}
void CEvacuationTable::content::Free(){
	delete this;
}
